package com.yunshu.yunshuapp;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.utils.DiskCacheUtils;
import com.nostra13.universalimageloader.utils.MemoryCacheUtils;
import com.yunshu.yunshuapp.kuaixun.KuaixunListModel;
import com.yunshu.yunshuapp.network.YSCallback;
import com.yunshu.yunshuapp.network.YSRequest;
import com.yunshu.yunshuapp.user.LoginActivity;
import com.yunshu.yunshuapp.user.UserInfoActivity;
import com.yunshu.yunshuapp.user.YSUser;
import com.yunshu.yunshuapp.utils.YSUtils;

import java.io.IOException;
import java.util.Set;

public class SettingActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        NavigationBar bar = (NavigationBar) findViewById(R.id.navigationBar);
        bar.putInActivity(this);
        bar.setTitle("设置");

        PackageInfo packageInfo = null;
        try {
            packageInfo = getPackageManager()
                    .getPackageInfo(getPackageName(), 0);
            //获取APP版本versionName
            String versionName = packageInfo.versionName;
            TextView versionTV = (TextView) findViewById(R.id.versionTV);
            versionTV.setText("云数智惠V"+versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }


        addTapListener();
    }

    private void addTapListener() {

        View view = findViewById(R.id.agreement);
        view.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(SettingActivity.this,WebviewActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("url",YSRequest.AGREEMENT);
                intent.putExtras(bundle);
                startActivityForResult(intent,0);
            }
        });

        View view2 = findViewById(R.id.feedback);
        view2.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(SettingActivity.this,FeedbackActivity.class);
                startActivityForResult(intent,0);
            }
        });

        View view3 = findViewById(R.id.version);
        view3.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                checkVersion();
            }
        });

        View view4 = findViewById(R.id.cache);
        view4.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                clearCache();
            }
        });

        View view5 = findViewById(R.id.tel400);
        view5.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                String tel = "tel:" + SettingActivity.this.getString( R.string.TEL_400);
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(tel)); //ACTION_CALL
                //开启系统拨号器
                startActivity(intent);
            }
        });

    }

    private void checkVersion() {
        YSRequest request = new YSRequest(this);
        request.GET(YSRequest.checkUpdate,"", new YSCallback() {

            @Override
            public void onResponse(int isSuccess, JsonNode result) {
                if (isSuccess != YSRequest.REQUEST_OK) return;
                ObjectMapper mapper = new ObjectMapper();
                String jsonstr = result.toString();
                int newVersion = Integer.parseInt(jsonstr);
                PackageInfo packageInfo = null;
                try {
                    packageInfo = getPackageManager()
                            .getPackageInfo(getPackageName(), 0);
                    //获取APP版本versionName
                    String versionName = packageInfo.versionName;
                    //获取APP版本versionCode
                    int versionCode = packageInfo.versionCode;
                    if (newVersion > versionCode){
                        needShengji();
                    }else{
                        YSUtils.showToastOn(SettingActivity.this,"当前已经是最新版本！");

                    }
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void clearCache() {

        ImageLoader.getInstance().clearMemoryCache();//清除内存
        ImageLoader.getInstance().clearDiskCache();//清除sd卡

        YSUtils.showToastOn(this, "缓存清理完毕");

    }

    private void needShengji() {

        new AlertDialog.Builder(this).setTitle("提示")//设置对话框标题

                .setMessage("发现新版本，建议您立刻点击\"确定\"，进行升级")//设置显示的内容 ;
                .setPositiveButton("确定",new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                        gotoUpdate();
                    }
                }).setNegativeButton("取消",new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件

                    }
                }).show();
    }

    private void gotoUpdate() {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse("http://a.app.qq.com/o/simple.jsp?pkgname=com.yunshu.yunshuapp");
        intent.setData(content_url);
        startActivity(intent);
    }
}
